//
//  MonthCollectionCell.m
//  TimeTest
//
//  Created by 王鹏宇 on 2018/1/26.
//  Copyright © 2018年 LvJianfeng. All rights reserved.
//

#import "MonthCollectionCell.h"
#import "NSDate+Formatter.h"
@implementation MonthCollectionCell

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self.contentView addSubview:self.collectionView];
    }
    return self;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self.contentView addSubview:self.collectionView];
        self.contentView.backgroundColor = [UIColor yellowColor];
    }
    return self;
}
- (void)configdata:(NSMutableArray *)dateArray SelectDate:(NSDate *)selectedDate{
    self.daysArray = [NSMutableArray arrayWithArray:dateArray];
    [self selectTheDate:selectedDate];
    [self.collectionView reloadData];
}

- (void)selectTheDate:(NSDate *)selectedDate {
    
    if (!selectedDate) {
        return;
    }
    for (MonthModel * mon in self.daysArray) {
        
        if ([mon isKindOfClass:[MonthModel class]]) {
            if ([mon.dateValue compare:selectedDate] == NSOrderedSame) {
                mon.isSelectedDay = YES;
            }else{
                mon.isSelectedDay = NO;
            }
        }
    
    }
}
//-(void)configData:(NSDate *)date  StartDate:(NSDate *)startDate EndDate:(NSDate *)endDate SelectDate:(NSDate *)selectDate{
//    self.startDate = startDate;
//    self.endDate = endDate;
//    self.tempDate = date;
//    self.selectDate = selectDate;
////    [self getDataDayModel:date];
//    [self.collectionView reloadData];
//}


- (UICollectionView *)collectionView {
    
    if (!_collectionView) {
       
        CGFloat width = self.frame.size.width / 7;
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.itemSize = CGSizeMake(width, width);
        flowLayout.headerReferenceSize = CGSizeMake(self.frame.size.width, 30);
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,0,self.frame.size.width, width * 6  + 30 ) collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        [_collectionView registerClass:[CalendarCell class] forCellWithReuseIdentifier:@"CalendarCell"];
        [_collectionView registerClass:[CalendarHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CalendarHeaderView"];
        
    }
    return _collectionView;
}





#pragma mark  --- UICollectionViewDelegate,UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.daysArray.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CalendarCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CalendarCell" forIndexPath:indexPath];
    cell.dayLabel.backgroundColor = [UIColor whiteColor];
    cell.dayLabel.textColor = kTitleColor;
    id mon = self.daysArray[indexPath.row];
    if ([mon isKindOfClass:[MonthModel class]]) {
        cell.monthModel = (MonthModel *)mon;
    }else{
        cell.dayLabel.text = @"";
    }
    
    return cell;
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    CalendarHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"CalendarHeaderView" forIndexPath:indexPath];
    return headerView;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    id mon = self.daysArray[indexPath.row];
    if ([mon isKindOfClass:[MonthModel class]]) {
        
        MonthModel * model = mon;
        if (model.isCanSelect) {
            
            if (_delegate && [_delegate respondsToSelector:@selector(selectDate:)]) {
                [_delegate selectDate:model.dateValue];
            }
        }
    }
}


@end


@implementation CalendarHeaderView
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        CGFloat width = self.frame.size.width / 7;
        NSArray *weekArray = [[NSArray alloc] initWithObjects:MYLocalizedString(@"TICKET_WEEK_SUN", nil),MYLocalizedString(@"TICKET_WEEK_MON", nil),MYLocalizedString(@"TICKET_WEEK_TUE", nil),MYLocalizedString(@"TICKET_WEEK_WED", nil),MYLocalizedString(@"TICKET_WEEK_THU", nil),MYLocalizedString(@"TICKET_WEEK_FRI", nil),MYLocalizedString(@"TICKET_WEEK_SAT", nil), nil];
        
        for (int i=0; i<weekArray.count; i++) {
            UILabel *weekLabel = [[UILabel alloc] initWithFrame:CGRectMake(i * width,0, width, 30)];
           // weekLabel.backgroundColor = kBGColor;
            weekLabel.textAlignment = NSTextAlignmentCenter;
            weekLabel.textColor = [UIColor grayColor];
            weekLabel.font = [UIFont systemFontOfSize:13.f];
            weekLabel.text = weekArray[i];
            [self addSubview:weekLabel];
            self.backgroundColor = kBGColor;
        }
        
    }
    return self;
}
@end


@implementation CalendarCell
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        CGFloat width = self.contentView.frame.size.width*0.6;
        CGFloat height = self.contentView.frame.size.height*0.6;
        UILabel *dayLabel = [[UILabel alloc] initWithFrame:CGRectMake( self.contentView.frame.size.width*0.5-width*0.5,  self.contentView.frame.size.height*0.5-height*0.5, width, height )];
        dayLabel.textAlignment = NSTextAlignmentCenter;
        dayLabel.layer.masksToBounds = YES;
        dayLabel.layer.cornerRadius = height * 0.5;
        
        [self.contentView addSubview:dayLabel];
        self.dayLabel = dayLabel;
        
    }
    return self;
}

- (void)setMonthModel:(MonthModel *)monthModel{
    _monthModel = monthModel;
    self.dayLabel.text = [NSString stringWithFormat:@"%02ld",monthModel.dayValue];
    self.dayLabel.alpha = 1.0f;
   
    if (!monthModel.isCanSelect) {
        self.dayLabel.textColor = kContentColor;
        self.dayLabel.alpha = 0.5;
    }
    if (monthModel.isToday) {
        self.dayLabel.textColor = kThemeColor;
        self.dayLabel.alpha = 1.0f;
    }
    if (monthModel.isSelectedDay) {
        self.dayLabel.backgroundColor = kThemeColor;
        self.dayLabel.textColor = [UIColor whiteColor];
    }
}

@end


@implementation MonthModel

@end
